import { reactive, ref, unref } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { ERR_OK } from 'config/statusCode'
import { isArray } from 'utils/validate'
import { deepClone } from '@/utils/index'

// const emit = defineEmits(['update:modelValue', handelChange])
/* eslint-disable */
export function mixinsData(columnDatalist, itemDataObj, rulesDataObj) {
  const columnData = ref(columnDatalist)
  const itemData = ref(itemDataObj)
  const rulesData = rulesDataObj

  // modelValue value值返回父组件
  function handelBlur() {
    if (columnData.value.requiredFlag) {
      itemData.value.isError = forItem(itemData.value, rulesData, columnData.value.fieldCode)
    }
  }
  //  input 输入框值变化是返回父组件的值
  function handelChange(val) {
    itemData.value[columnData.value.fieldCode] = val
    if (columnData.value.requiredFlag) {
      itemData.value.isError = forItem(itemData.value, rulesData, columnData.value.fieldCode)
    }
  }

  const forItem = (item, rules, key = null) => {
    let json = {}
    if (key) {
      if (item[key] !== '') {
        if (rules[key] && !rules[key].regular.test(item[key])) {
          json = {
            [key]: {
              status: false,
              msg: rules[key].msg
            },
            ...json
          }
        } else {
          json = {
            [key]: {
              status: true,
              msg: ''
            },
            ...json
          }
        }
      } else if (item[key] === '') {
        json = {
          [key]: {
            status: false,
            msg: '不能为空，请输入'
          },
          ...json
        }
      }
      json = { ...item.isError, ...json }
    } else {
      for (let k in item.isError) {
        if (k !== 'isError' && k !== 'isEdit') {
          if (item[k] !== '') {
            if (rules[k] && !rules[k].regular.test(item[k])) {
              json = {
                [k]: {
                  status: false,
                  msg: rules[k].msg
                },
                ...json
              }
            } else {
              json = {
                [k]: {
                  status: true,
                  msg: ''
                },
                ...json
              }
            }
          } else if (item[k] === '') {
            json = {
              [k]: {
                status: false,
                msg: '不能为空，请输入'
              },
              ...json
            }
          }
        }
      }
    }
    return json
  }
  return {
    handelBlur,
    handelChange,
    forItem
  }
}
